//
//  CRecentlyContactMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/25.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CRecentlyContactMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CRecentlyContactModel.h"
@implementation CRecentlyContactMgr
//插入最近联系人
-(BOOL)insertRecentlyContact:(CRecentlyContactModel *)recentlyContact
{
    if(recentlyContact == nil || M_ISSTRNULL(recentlyContact.userId))
    {
        return NO;
    }
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"INSERT INTO RECENTLYCONTACT(UserId,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?,?,?,?)", M_STOREVALUE(recentlyContact.userId),M_STOREVALUE(recentlyContact.yuLiuZiduan1),@(recentlyContact.yuLiuZiduan2),M_STOREVALUE(recentlyContact.yuLiuZiduan3)];
        
    }];
    return ret;
}
//批量插入最近联系人
-(BOOL)batchInsertRecentlyContact:(NSArray *)recentlyContactList
{
    if(recentlyContactList == nil || [recentlyContactList count] <= 0)
    {
        return NO;
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CRecentlyContactModel *recentlyContact in recentlyContactList)
        {
            if(recentlyContact != nil && !M_ISSTRNULL(recentlyContact.userId))
            {
                ret = [db executeUpdate:@"INSERT INTO RECENTLYCONTACT(UserId,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?,?,?,?)", M_STOREVALUE(recentlyContact.userId),M_STOREVALUE(recentlyContact.yuLiuZiduan1),@(recentlyContact.yuLiuZiduan2),M_STOREVALUE(recentlyContact.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                    break;
                }
            }
        }
        
    }];
    return ret;
}
//删除最近联系人
-(BOOL)delRecentlyContact:(NSString *)UserId
{
    if(M_ISSTRNULL(UserId))
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from RECENTLYCONTACT where UserId = ?", UserId];
    }];
    return ret;
}
//加载所有最近联系人
-(NSArray *)loadAllRecentlyContacts
{
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from RECENTLYCONTACT" ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CRecentlyContactModel *recentlyContact = [[CRecentlyContactModel alloc] init];
            recentlyContact.userId = [result stringForColumn:@"UserId"];
            recentlyContact.RecentlyContactIndex = [result longLongIntForColumn:@"identifer"];
            [array addObject:recentlyContact];
            recentlyContact = nil;
        }
        [result close];
    }];
    return array;
}

- (CRecentlyContactModel *)loadRecentlyContactWithUserId:(NSString *)userId
{
    if (M_ISSTRNULL(userId)) {
        return nil;
    }
    __block CRecentlyContactModel *recentlyContact = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from RECENTLYCONTACT where userId = ?",userId];
        while ([result next]) {
           recentlyContact = [[CRecentlyContactModel alloc] init];
            recentlyContact.userId = [result stringForColumn:@"UserId"];
            recentlyContact.RecentlyContactIndex = [result longLongIntForColumn:@"identifer"];
        }
        [result close];
    }];
    return recentlyContact;

}

- (BOOL)delAllRecentlyContacts
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from RECENTLYCONTACT"];
    }];
    return ret;

}

@end
